package com.ynny.chapter07_3.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/file")
public class UploadController {

    @Value("D:/upload/")
    private String uploadPath;

    @PostMapping("/upload")
    public String upload(@RequestParam("file")MultipartFile file) throws IOException {

        String uuid = UUID.randomUUID().toString();
        //D:/upload/uuid-文件名
        File newFile = new File(
                uploadPath +
                uuid + "-" +
                file.getOriginalFilename());

        file.transferTo(newFile);

        return "/upload/"+ uuid + "-" +
                file.getOriginalFilename();
    }

}
